package com.jack.leetcode.string;

import java.util.ArrayList;
import java.util.List;

/***
 * @author zhangjie
 * @date 2020/5/6 22:08
 */
public class Z字形变换 {

    public static void main(String[] args) {
        System.out.println(convert("LEETCODEISHIRING",3));
        System.out.println(convert("LEETCODEISHIRING",4));
    }

    public static String convert(String s, int numRows) {
        if (numRows < 2) {
            return s;
        }
        StringBuilder result = new StringBuilder();
        List<StringBuilder> arr = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            arr.add(new StringBuilder());
        }
        int i = 0, flag = -1;
        for (char c : s.toCharArray()) {
            arr.get(i).append(c);
            if (i == 0 || i == numRows - 1) {
                flag = -flag;
            }
            i += flag;
        }
        for(StringBuilder stringBuilder:arr){
            result.append(stringBuilder);
        }
        return result.toString();
    }
}
